/**
 * @(#)ReflectionUtil.java, 2015年3月31日. Copyright 2012 Yodao, Inc.
 *                                     All rights reserved. YODAO
 *                                     PROPRIETARY/CONFIDENTIAL. Use is subject
 *                                     to license terms.
 */
package com.scitc.pleasurevideo.util;

import com.scitc.pleasurevideo.data.ETMan;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.lang.reflect.Method;
import java.net.URLEncoder;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

/**
 * @author lukun
 * 
 */
public class ReflectionUtil {
	public static Object fillObjectByReflect(Class inputClass,
                                             Map<String, Object> datas) {
		Object result = null;
		try {
			String json = ETMan.getMananger().getGson().toJson(datas);
			Object obj = ETMan.getMananger().getGson()
					.fromJson(json, inputClass);
            return obj;
		} catch (Exception e) {
		}
		return result;
	}

	public static List fillMapByReflect(Class inputClass,
                                        Map<String, Object> datas) {
		Map<String, Object> map = new HashMap<String, Object>();
		List results = new ArrayList();
		try {
			Object list = datas.get("topDatas");
			if(list == null){
			    list = datas.get("datas");
			}
			if(list == null){
			    list = datas.get("data");
			}
			if (list instanceof List) {
				List dataList = (List) list;
				for (Object o : dataList) {
					Map<String, Object> m = (Map) o;
					results.add(fillObjectByReflect(inputClass,
							(Map<String, Object>) m));
				}
			}
			return results;
		} catch (Exception e) {
		}
		return results;
	}

	public static Map<String, Object> fillMapByReflect(Class inputClass,
                                                       JSONObject json) {
		Map<String, Object> map = new HashMap<String, Object>();
		if (json == null) {
			return map;
		}

		try {
			Iterator it = json.keys();
			while (it.hasNext()) {
				String key = (String) it.next();
				Object value = json.get(key);
				if (value instanceof Long) {
					map.put(key, (Long) value);
				} else if (value instanceof Integer) {
					map.put(key, (Integer) value);
				} else if (value instanceof String) {
					map.put(key, (String) value);
				}
				if ("datas".equalsIgnoreCase(key)) {
					if (value instanceof JSONArray) {
						JSONArray array = (JSONArray) value;
						List<Object> datas = new ArrayList<Object>();
						for (int i = 0; i < array.length(); i++) {
							Object obj = array.get(i);
							if (obj instanceof JSONObject) {
								datas.add(fillObjectByReflect(inputClass,
										(JSONObject) obj));
							} else {
								datas.add(obj);
							}
						}
						map.put("datas", datas);
					}
				}
				if ("headdatas".equalsIgnoreCase(key)) {
					if (value instanceof JSONArray) {
						JSONArray array = (JSONArray) value;
						List<Object> datas = new ArrayList<Object>();
						for (int i = 0; i < array.length(); i++) {
							Object obj = array.get(i);
							if (obj instanceof JSONObject) {
								datas.add(fillObjectByReflect(inputClass,
										(JSONObject) obj));
							} else {
								datas.add(obj);
							}
						}
						map.put("headdatas", datas);
					}
				}
			}
		} catch (Exception e) {
		}
		return map;
	}

	public static Object fillObjectByReflect(Class inputClass, JSONObject json)
			throws JSONException, InstantiationException,
            IllegalAccessException {
		Object result = inputClass.newInstance();
		Iterator it = json.keys();
		Method methods[] = inputClass.getMethods();
		try {
			for (Method method : methods) {
				String methodName = method.getName();
				if (methodName.startsWith("set")) {
					methodName = methodName.substring(3, 4).toLowerCase()
							+ methodName.substring(4);
					if (!json.isNull(methodName)) {
						Object value = json.get(methodName);
						if (value == null) {
							continue;
						}
						Class injectsClass[] = method.getParameterTypes();
						if (injectsClass != null && injectsClass.length == 1) {
							if (injectsClass[0]
									.isAssignableFrom(Long.class)) {
								value = Long.parseLong(String.valueOf(value));
							} else if (injectsClass[0]
									.isAssignableFrom(Integer.class)) {
								value = Integer.parseInt(String.valueOf(value));
							}
							method.invoke(result, value);
						}

					}
				}
			}
		} catch (Exception e) {
		}
		return result;
	}




}
